// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObjectDetectorPicknPlaceIntegratorAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Manipulation
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Manipulation.PicknPlace;
    using Microsoft.Robotics.Manipulation.Runtime;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Tracing;
    using Microsoft.Robotics.Vision;
    using Microsoft.Robotics.Vision.ObjectDetection;
    using Microsoft.Robotics.Vision.Runtime;

    /// <summary>
    /// Integrates object detection with pick and place
    /// This implementation compares the visual detected pose
    /// estimates with truth from the test harness and fails cases where the estimate is 
    /// either of low confidence or deviates significantly from the test value.
    /// </summary>
    [DataContract]
    public class ObjectDetectorPicknPlaceIntegratorAgent : ConsumerProducerAgent<PicknPlaceStatusMessage, PicknPlaceStatusMessage>,
                                        IControlReceiver<PickAndMoveMessage>,
                                        IControlReceiver<PlaceAndMoveMessage>,
                                        ISubscriptionReceiver<KinectObjectDetectorResultIRMessage>,
                                        IMessagePublisher<KinectObjectDetectorResultIRMessage>
    {
        /// <summary>
        /// Object detector agent
        /// </summary>
        [DataMember(Name = "ObjectDetectorAgentName")]
        private string objectDetectorAgentName;

        /// <summary>
        /// The data collection objection detection agent. If null or empty means there is no data 
        /// collection agent
        /// </summary>
        [DataMember(Name = "ObjectDetectorDataCollectionAgentName")]
        private string objectDetectorDataCollectionAgentName;

        /// <summary>
        /// Pick Place agent to control
        /// </summary>
        [DataMember(Name = "PickPlaceAgentName")]
        private string pickPlaceAgentName;

        /// <summary>
        /// Minimum confidence of top estimate to be considered
        /// </summary>
        [DataMember(Name = "MinConfidence")]
        private double minConfidence;

        /// <summary>
        /// Maximum number of tries before declare failure
        /// </summary>
        [DataMember(Name = "MaxRetry")]
        private int maxRetry;

        /// <summary>
        /// Minimum number of detection results before checking result
        /// </summary>
        [DataMember(Name = "MinDetectionCount")]
        private int minDetectionCount;

        /// <summary>
        /// Master data log file
        /// </summary>
        [DataMember(Name = "MasterLogPathName")]
        private string masterLogPathName;

        /// <summary>
        /// Path to kinect data files. For example d:\dataFile\Kinect.txt
        /// The final name is constructed as  d:\dataFile\Kinect_dateStamp.txt
        /// </summary>
        [DataMember(Name = "KinectFilesPath")]
        private string kinectFilesPath;

        /// <summary>
        /// List of expected objects. Fail if all are not found in the
        /// the ground truth message
        /// </summary>
        [DataMember(Name = "ExpectedObjects")]
        private List<string> expectedObjects;

        /// <summary>
        /// Control port for controlling  pick and move publishers
        /// </summary>
        private IForwarder<PickAndMoveMessage> pickMovePublisher;

        /// <summary>
        /// Control port for controlling  pick and move publishers
        /// </summary>
        private IForwarder<PlaceAndMoveMessage> placeMovePublisher;

        /// <summary>
        /// Debug port on object detector for status messages
        /// </summary>
        private IForwarder<PicknPlaceStatusMessage> objectDetectorDebugPort;

        /// <summary>
        /// Forwarder to re-publish object detection messages
        /// </summary>
        private IForwarder<KinectObjectDetectorResultIRMessage> objectDetectionRepublish;

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDetectorPicknPlaceIntegratorAgent" /> class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="objectDetectorAgentName">Object detector agent name</param>
        /// <param name="objectDetectorDataCollectionAgentName">Name of the data collection detector agent. Can be null or empty which  means there is no collection agent</param>
        /// <param name="minConfidence">Minimum detection confidence</param>
        /// <param name="maxRetry">Maximum number of detection retries</param>
        /// <param name="minDetectionCount">Minimum number of results before checking result validity</param>
        public ObjectDetectorPicknPlaceIntegratorAgent(
            string name,
            IProducer<PicknPlaceStatusMessage> producer,
            string objectDetectorAgentName,
            string objectDetectorDataCollectionAgentName,
            double minConfidence,
            int maxRetry,
            int minDetectionCount)
            : base(name, producer.Name)
        {
            this.objectDetectorAgentName = objectDetectorAgentName;
            this.objectDetectorDataCollectionAgentName = objectDetectorDataCollectionAgentName;
            this.pickPlaceAgentName = producer.Name;
            this.minConfidence = minConfidence;
            this.maxRetry = maxRetry;
            this.minDetectionCount = minDetectionCount;
            this.kinectFilesPath = null;
            this.masterLogPathName = null;
            this.expectedObjects = null;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDetectorPicknPlaceIntegratorAgent" /> class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="objectDetectorAgentName">Object detector agent name</param>
        /// <param name="objectDetectorDataCollectionAgentName">Name of the data collection detector agent. Can be null or empty which  means there is no collection agent</param>
        /// <param name="minConfidence">Minimum detection confidence</param>
        /// <param name="maxRetry">Maximum number of detection retries</param>
        /// <param name="minDetectionCount">Minimum number of results before checking result validity</param>
        /// <param name="expectedObjects">List of objects expected</param>
        /// <param name="masterLogPathName">Path to master log file</param>
        /// <param name="kinectFilesPath">Path name for kinect data files</param>
        public ObjectDetectorPicknPlaceIntegratorAgent(
            string name,
            IProducer<PicknPlaceStatusMessage> producer,
            string objectDetectorAgentName,
            string objectDetectorDataCollectionAgentName,
            double minConfidence,
            int maxRetry,
            int minDetectionCount,
            List<string> expectedObjects,
            string masterLogPathName,
            string kinectFilesPath)
            : base(name, producer.Name)
        {
            this.objectDetectorAgentName = objectDetectorAgentName;
            this.objectDetectorDataCollectionAgentName = objectDetectorDataCollectionAgentName;
            this.pickPlaceAgentName = producer.Name;
            this.minConfidence = minConfidence;
            this.maxRetry = maxRetry;
            this.minDetectionCount = minDetectionCount;
            this.kinectFilesPath = kinectFilesPath;
            this.masterLogPathName = masterLogPathName;
            this.expectedObjects = expectedObjects;
        }

        /// <summary>
        /// Gets the detection sequencer FSM context
        /// </summary>
        internal ObjectDetectorPicknPlaceIntegratorState State { get; private set; }

        /// <summary>
        /// Initializes the agent
        /// </summary>
        /// <param name="locator">Agent locator to use</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            IAgentHost hardwareAgentLocator = locator.GetAgent(this.objectDetectorAgentName);
            hardwareAgentLocator.Subscribe<KinectObjectDetectorResultIRMessage>(this);

            if (false == string.IsNullOrEmpty(this.objectDetectorDataCollectionAgentName))
            {
                IAgentHost dataCollectionAgentLocator = locator.GetAgent(this.objectDetectorDataCollectionAgentName);
                this.objectDetectorDebugPort = dataCollectionAgentLocator.GetControlPort<PicknPlaceStatusMessage>();
            }

            IAgentHost pickPlaceHost = locator.GetAgent(this.pickPlaceAgentName);
            this.pickMovePublisher = pickPlaceHost.GetControlPort<PickAndMoveMessage>();
            this.placeMovePublisher = pickPlaceHost.GetControlPort<PlaceAndMoveMessage>();

            this.State = new ObjectDetectorPicknPlaceIntegratorState(this.minDetectionCount, this.maxRetry, this.masterLogPathName, this.kinectFilesPath);
        }

        /// <summary>
        /// Called when the agent is about to start running
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();
        }

        /// <summary>
        /// Called when the agent is about to end
        /// </summary>
        public override void OnDeactivated()
        {
            base.OnDeactivated();
        }

        /// <summary>
        /// Handles reception of the ManipulatorStatusMessage message 
        /// just republish it
        /// </summary>
        /// <param name="message">Received kinect message</param>
        public override void Receive(PicknPlaceStatusMessage message)
        {
            PicknPlaceStatusMessage republishMessage;

            if (true == this.State.IsVisualDetectionFail && PicknPlaceModeEnum.VisualObjectDetection == this.State.LastPickMode)
            {
                // Failed create a fail message
                republishMessage = new PicknPlaceStatusMessage(
                    message,
                    this.State.LastStatusString,
                    this.State.LastPickStatus,
                    message.ManipulatedObjectDescription,
                    message.PickPlacePose,
                    message.RetractPose);
            }
            else
            {
                PicknPlaceStatusEnum status = (this.State.LastPickStatus == PicknPlaceStatusEnum.PickRequestReceived) ? this.State.LastPickStatus : message.StatusEnum;
                
                // Framework requires a copy of the original message
                republishMessage = new PicknPlaceStatusMessage(
                    message,
                    message.StatusString,
                    status,
                    message.ManipulatedObjectDescription,
                    message.PickPlacePose,
                    message.RetractPose);
            }

            this.Publisher.Post(republishMessage);

            if (null != this.objectDetectorDebugPort)
            {
                this.objectDetectorDebugPort.Post(republishMessage);
            }
        }

        /// <summary>
        /// Handle requests for pick and and move requests. The default behavior is to 
        /// mark the request as pending on visual detection results. In legacy mode
        /// the request is simply forwarded
        /// </summary>
        /// <param name="msg">Message to process</param>
        public void ReceiveControl(PickAndMoveMessage msg)
        {
            if (msg.State.Length <= 0)
            {
                TraceOut.Warning(TraceContexts.Manip, "Integrator PickFrom received {0} objects", msg.State.Length);
                return;
            }

            foreach (PickAndMove pickMove in msg.State)
            {
                Vector3 p = pickMove.PickFromPose.Position;
                Vector3 r = pickMove.MoveToPose.Position;
                TraceOut.Info(TraceContexts.Manip, "Integrator PickFrom: {0} {1:F3} {2:F3} {3:F3}  RetractTo {4:F3} {5:F3} {6:F3}", pickMove.ManipulatedObjectDescription.Description, p.X, p.Y, p.Z, r.X, r.Y, r.Z);
            }

            if (PicknPlaceModeEnum.Baseline == msg.State[0].PickMode)
            {
                // Legacy mode
                this.pickMovePublisher.Post(msg);
            }
            else
            {
                this.State.NewPickMessageReceived(msg.State, msg.OriginatingTime, this.expectedObjects);
            }
        }

        /// <summary>
        /// Handle requests for pick and and move. Integrate with the object detector
        /// </summary>
        /// <param name="placeMessage">Message to process</param>
        public void ReceiveControl(PlaceAndMoveMessage placeMessage)
        {
            if (placeMessage.State.Length <= 0)
            {
                TraceOut.Warning(TraceContexts.Manip, "Integrator PlaceTo received {0} objects", placeMessage.State.Length);
                return;
            }

            foreach (PlaceAndMove pm in placeMessage.State)
            {
                Vector3 p = pm.PlacePose.Position;
                Vector3 r = pm.RetractPose.Position;
                TraceOut.Info(TraceContexts.Manip, "Integrator PlaceTo: {0} {1:F3} {2:F3} {3:F3}  RetractTo {4:F3} {5:F3} {6:F3}", pm.ManipulatedObjectDescription.Description, p.X, p.Y, p.Z, r.X, r.Y, r.Z);
            }

            PlaceAndMove placeMove = placeMessage.State[0];

            this.State.NewPlaceMessageReceived(placeMove.PlacePose, placeMove.RetractPose, placeMove.ManipulatedObjectDescription);
            this.placeMovePublisher.Post(placeMessage);
        }

        /// <summary>
        /// Handle reception of object detection messages
        /// </summary>
        /// <param name="message">The object detection message</param>
        public void Receive(KinectObjectDetectorResultIRMessage message)
        {
            PickAndMove[] pickMoves;
            if (true == this.State.IsNewDectionSuccess(message, out pickMoves) && PicknPlaceModeEnum.VisualObjectDetection == this.State.LastPickMode)
            {
                this.PostPickFrom(pickMoves);
            }

            this.objectDetectionRepublish.Post(new KinectObjectDetectorResultIRMessage(message.OriginatingTime, null, null, message.State));
        }

        /// <summary>
        /// Initializer to object detection results
        /// </summary>
        /// <param name="forwarder">A forwarder that can be used to broadcast our state.</param>
        public void InitializePublisher(IForwarder<KinectObjectDetectorResultIRMessage> forwarder)
        {
            this.objectDetectionRepublish = forwarder;
        }

        /// <summary>
        /// Publishes a new version of the last pick and move message
        /// to the pick and place agent. Generally this should publish
        /// when visual detection has succeeded.
        /// </summary>
        /// <param name="pickMove">Pick and move requests</param>
        private void PostPickFrom(PickAndMove[] pickMove)
        {
            PickAndMoveMessage msg = new PickAndMoveMessage(AgentMessage.GetCurrentTime(), pickMove);
            this.pickMovePublisher.Post(msg);
        }
    }
}
